package com.ivan.controller;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import com.ivan.domain.Account;
import com.ivan.domain.Messages;
import com.ivan.service.AccountService;
import com.ivan.service.MessageService;

import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MessageController {

	protected static Logger logger = Logger.getLogger("controller");
	
	@Resource(name="accountService")
	private AccountService accountService;
	@Resource(name="messageService")
	private MessageService messageService;
	
	private List<Account> accounts;
	private List<Messages> messages;
	private int accountid = -1;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView addMessage(@ModelAttribute("mensahe") Messages mensahe ) {
		System.out.println(mensahe.getMessage()+" MEsSAGE");
		ModelAndView mav = new ModelAndView("blogpage");
		mav.addObject("mensahe", new Messages());
		messageService.add(mensahe);
		logger.debug("Received request to add new message");
	    mav.addObject("msg",mensahe.getAccountid());
	    accounts = accountService.getAllAccounts();
	    mav.addObject("accounts", accounts);
	    messages =messageService.getAllMessages();
	    mav.addObject("messages", messages);
	    return mav;
	}

	 @RequestMapping(value = "/edit", method = RequestMethod.GET)
     public ModelAndView getEditMessage(@RequestParam(value="id", required=true) Integer id) {
		 ModelAndView mav = new ModelAndView("editblog");
	     logger.debug("Received request to show edit message page");
	     mav.addObject("messageAttribute", messageService.get(id));
	     mav.addObject("mensahe", new Messages());
	     return mav;
	 }
	 
	 @RequestMapping(value = "/edit", method = RequestMethod.POST)
     public ModelAndView saveEditMessage( @RequestParam(value="id", required=true) Integer id, Model model,HttpServletRequest request, HttpServletResponse response) {
		 ModelAndView mav = new ModelAndView("blogpage");
		 mav.addObject("mensahe", new Messages());
	     logger.debug("Received request to update message");
	     String accid = request.getParameter("accountid");
	     String msgid = request.getParameter("messageid");
	     String mssg = request.getParameter("message");
	     Messages msg = new Messages();
	     msg.setAccountid(Integer.parseInt(accid));
	     msg.setMessageid(Integer.parseInt(msgid));
	     msg.setMessage(mssg);
	     messageService.edit(msg);
	     mav.addObject("msg",accid);
	     messages =messageService.getAllMessages();
	     mav.addObject("messages", messages);
	     accounts = accountService.getAllAccounts();
	     mav.addObject("accounts", accounts);
	     return mav;
	 }			
}
